import request from '@/utils/request'
import { useUserStore } from '@/stores'

// const baseURL = 'http://big-event-vue-api-t.itheima.net'

// 获取数据库信息  
export const getData = () => 
  request.get("/getUser");
// 获取配置信息
export const getConfig = () => request.get("/getConfig");
// 更新配置信息
export const updateConfig = (config) => request.post("/updateConfig", { config });
// 获取按钮信息
export const getButton = () => request.get("/getButton");
// 获取app日志 
export const getAppLog = (appLog) => request.post("/getAppLog", { appLog });
// 获取app状态
export const getAppStatus = (app) => request.post("/getAppStatus", { app });
// 开启APP
export const startApp = (appPath) => request.post("/startApp", { appPath })
// 停止APP
export const stopApp = (appPath) => request.post("/stopApp", { appPath });
// 注册接口
export const userRegisterService = ({ account, password, username }) =>
  request.post('/register', { account, password, username })

// 登录接口 3000
export const userLoginService = ({ account, password, key }) =>
  request.post('/login', { account, password, key })
// 退出登录接口
export const loginOut = ({ account }) => request.post('/loginOut', { account })
// 验证token是否有效
export const ident = () => request.post('/identification')
// 获取路由信息
export const fetchPermission = (data) => request.post('/getRoutes', data)
// 获取操作权限
export const getOperationPermissions = () =>
  request.post('/getOperationPermissions')
export const getAccount = ({ account, name, time }) =>
  request.post('/user/getAccount', { account, name, time })
// 获取角色
// export const getAllRoute = () => request.get('/user/getAllRoute')
// 获取路由
export const getUserRole = () => request.get('/user/getUserRole')
// 获取路由信息
// eslint-disable-next-line space-before-function-paren
export const getUser = async ({ account }) => {
  const res = await request.post('/getUser', { account })
  if (res.data.code == 200 && res.data.data.length > 0) {
    const userStore = useUserStore()
    const data2 = res.data.data[0]
    const user = {
      userName: data2.Name,
      account: data2.NserName,
      permission: data2.Permission,
      email: data2.email,
      jobNumber: data2.JobNumber
    }
    // 直接将获取的用户基础信息更新到仓库
    userStore.setUser(user)
  }
}
export const userGetInfoService = () => request.get('/my/userinfo')

// 更新用户基本信息
export const userUpdateInfoService = ({
  account,
  userName,
  permission,
  email,
  jobNumber
}) =>
  request.put('/user/userinfo', {
    account,
    userName,
    permission,
    email,
    jobNumber
  })

// 更新用户头像
export const userUpdateAvatarService = (avatar, fileName) =>
  request.patch(
    '/user/updateAvatar',
    {
      avatar,
      fileName
    },
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )

// 更新用户密码
export const userUpdatePasswordService = (params) =>
  request.post('/user/updatepwd', params)

// 编辑账户信息
export function EditAccount(params) {
  return request({
    url: '/user/EditAccount',
    method: 'post',
    data: params
  })
}
// 删除一个账号
export function DeleteAccount(params) {
  return request({
    url: '/user/DeleteAccount',
    method: 'post',
    data: params
  })
}
